<template>
  <div>
    <div class="top-panel">
      <el-card>
        <el-form label-width="70px" label-position="right">
          <el-row :gutter="10">
            <el-col :span="5">
              <el-form-item label="加入日期">
                <el-date-picker
                    v-model="searchForm.joinTimeRange"
                    type="daterange"
                    range-separator="~"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="YYYY-MM-DD"
                    @change="loadDataList"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="邮箱" label-width="60px">
                <el-input
                    v-model="searchForm.emailFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter.native="loadDataList">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="设备ID" label-width="60px">
                <el-input
                    v-model="searchForm.lastUseDeviceIdFuzzy"
                    clearable
                    placeholder="支持模糊搜索"
                    @keyup.enter.native="loadDataList">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" :style="{paddingLeft:'10px'}">
              <el-button-group>
                <el-button type="success" @click="loadDataList()">查询</el-button>
              </el-button-group>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
    <!-- 内容区域 -->
    <el-card :style="{ height: 'calc(100vh - 192px)',marginTop:'10px'}">
      <template #header>
        <span>问题列表</span>
      </template>
      <Table
          ref="tableInfoRef"
          :columns="columns"
          :fetch="loadDataList"
          :dataSource="tableData"
          :options="tableOptions"
          :extHeight="tableOptions.extHeight"
      >
        <template #slotNickName="{ index,row }">
          {{ row.nickName + `(${row.sex === 1 ? '男' : '女'})` }}
        </template>
        <template #slotStatus="{ index,row }">
          <div>{{ row.status === 1 ? '启用' : '禁用' }}</div>
        </template>
        <template #slotOperation="{ index,row }">
          <div @click="updateStatus(row)">{{ row.status === 0 ? '启用' : '禁用' }}</div>
        </template>
      </Table>
    </el-card>
  </div>
</template>


<script setup>
import {ref, getCurrentInstance} from "vue";

const {proxy} = getCurrentInstance();
const api = {
  loadDataList: "/appUser/loadDataList",
  updateStatus: "/appUser/updateStatus"
}

const searchForm = ref({})

const tableData = ref({});
const tableOptions = ref({
  extHeight: 125,
})

const columns = [
  {
    label: "邮箱",
    prop: "email",
    width: 200
  },
  {
    label: "昵称",
    prop: "nickName",
    scopedSlots: "slotNickName",
    width: 150
  },
  {
    label: "加入时间",
    prop: "joinTime",
    width: 200
  },
  {
    label: "最后登录时间",
    prop: "lastLoginTime",
    width: 200
  },
  {
    label: "登录设备",
    prop: "lastUseDeviceId",
  },
  {
    label: "设备品牌",
    prop: "lastUseDeviceBrand",
    width: 100
  },
  {
    label: "最后登录IP",
    prop: "lastLoginIp",
    width: 180
  },
  {
    width: 90,
    label: "状态",
    prop: "status",
    scopedSlots: "slotStatus"
  },
  {
    width: 90,
    label: "操作",
    scopedSlots: "slotOperation"
  },
]

const tableInfoRef = ref();

const loadDataList = async () => {
  let params = {
    pageNo: tableData.value.pageNo,
    pageSize: tableData.value.pageSize,
    ...searchForm.value
  }
  if (params.joinTimeRange) {
    params.joinTimeStart = searchForm.value.joinTimeRange[0];
    params.joinTimeEnd = searchForm.value.joinTimeRange[1];
  }
  delete params.joinTimeRange;
  let result = await proxy.Request({
    url: api.loadDataList,
    params: params,
  })
  if (!result) {
    return;
  }
  tableData.value = result.data;
}

// 改变用户状态
const updateStatus = async (row) => {
  let result = await proxy.Request({
    url: api.updateStatus,
    params: {
      status: row.status === 1 ? 0 : 1,
      userId: row.userId
    },
  })
  if (!result) {
    return;
  }
  proxy.Message.success("修改用户状态成功")
  loadDataList();
}
</script>


<style scoped lang="scss">
.box-card {
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .tree-panel {
    overflow: auto;
    height: calc(100vh - 304px);
  }
}
</style>
